package com.kjlink.internal.workreport.helper;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * 工时项目的通用工具类
 *
 * @author Vic.xu
 * @since 2024-05-16 13:59
 */
public class ReportCommonHelper {

    public static final String YEAR_MONTH_FORMAT = "yyyy-MM";

    public static final String MONTH_DAY_FORMAT = "MM-dd";

    public static final DateTimeFormatter YEAR_MONTH_FORMAT_FORMATTER = DateTimeFormatter.ofPattern(YEAR_MONTH_FORMAT);

    public static final DateTimeFormatter MONTH_DAY_FORMAT_FORMATTER = DateTimeFormatter.ofPattern(MONTH_DAY_FORMAT);

    /**
     * 工时转人天
     */
    public static String toPersonDays(Integer usedTime) {
        if (usedTime == null) {
            return "-";
        }
        return String.format("%.2f人天", usedTime / 480.0);
    }

    /**
     * 分钟工时转小时
     */
    public static BigDecimal toHoursNumber(Integer minutes) {
        if (minutes == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal value = new BigDecimal(minutes / 60.0);
        // 判断是否为整数
        if (value.stripTrailingZeros().scale() <= 0) {
            value = value.stripTrailingZeros();
        } else if (value.stripTrailingZeros().scale() == 1) {
            value = value.setScale(1, RoundingMode.HALF_UP);
        } else {
            value = value.setScale(2, RoundingMode.HALF_UP);
        }
        return new BigDecimal(value.toPlainString());
    }

    /**
     * 小时工时转分钟
     */
    public static Integer toMinutesNumber(BigDecimal hours) {
        if (hours == null) {
            return 0;
        }
        return hours.multiply(new BigDecimal(60)).intValue();
    }

    /**
     * 工时转人天
     */
    public static BigDecimal toPersonDaysNumber(Integer usedTime) {
        if (usedTime == null) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(usedTime / 480.0).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取年月
     *
     * @param date LocalDateTime
     * @return 202405
     */
    public static int toMonthDate(LocalDateTime date) {
        int year = date.getYear();
        int month = date.getMonthValue();
        //形如202401  202412
        return toMonthDate(year, month);
    }

    /**
     * 2024-12 to  202412
     *
     * @param yearMonth 2024-12
     * @return
     */
    public static int toMonthDate(String yearMonth) {
        LocalDate date = LocalDate.parse(yearMonth + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int year = date.getYear();
        int month = date.getMonthValue();
        //形如202401  202412
        return toMonthDate(year, month);
    }

    /**
     * 获取年月
     *
     * @param year  2024
     * @param month 5
     * @return 202405
     */
    public static int toMonthDate(int year, int month) {
        return Integer.valueOf(year + StringUtils.leftPad(String.valueOf(month), 2, "0"));
    }

    /**
     * 获取两个日期之间的全部日期（包含起始日期和结束日期）
     *
     * @param start 起始日期（格式：yyyy-MM-dd）
     * @param end   结束日期（格式：yyyy-MM-dd）
     * @return 日期列表
     */
    public static List<LocalDate> getDateRange(LocalDate start, LocalDate end) {
        // 存储日期范围
        List<LocalDate> dateRange = new ArrayList<>();
        // 遍历日期范围
        LocalDate currentDate = start;
        while (!currentDate.isAfter(end)) {
            dateRange.add(currentDate);
            // 增加一天
            currentDate = currentDate.plusDays(1);
        }
        return dateRange;
    }


    public static void main(String[] args) {
        System.out.println(toPersonDays(520));
        System.out.println(toMonthDate("2024-12"));
        System.out.println(toHoursNumber(520));
        System.out.println(toMinutesNumber(new BigDecimal("4.5")));

        List<LocalDate> dateRange = getDateRange(LocalDate.now(), LocalDate.now().plusDays(2));
        for (LocalDate date : dateRange) {
            System.out.println(date);
        }
    }


}
